<?php

namespace zh;

use other\ShopingConn;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

class LpLog
{
    private  $cz_f = 'campaign_id';

    /**
     * 运行
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function run()
    {
        // 每次处理最近三天
        for ($i = 0;$i>=-3;$i--)
        {
            $id1 = [];
            $id0 = [];
            $cdate = date('ymd', strtotime($i . ' day'));

            $sql = "select id,ip,page_type,template_id,host_id,goods_id,".$this->cz_f." from  (SELECT id,ip,host_id,goods_id,page_type,template_id,".$this->cz_f." , DATE_FORMAT(us_at, '%y%m%d') AS cdate from ".DbName::IP_LOGS." order by id asc) as a where cdate=".$cdate;

            $query = ZhConn::getConn()->query($sql);

            if ($query){
                foreach ($query as $row){
                    $key = $row['template_id'].'-'.$row['page_type'].'-'.$row['host_id'].'-'.$row['goods_id'];
//                    if ($row[$this->cz_f]){
//                        $key = $key.'-'.$row[$this->cz_f];
//                    }

                    // 如果存在，且数组里有该ip，则处理
                    if (isset($qc_arr[$key]) && (in_array($row['ip'],$qc_arr[$key]))){
                        $id0[] = $row['id'];
                    }else{
                        $qc_arr[$key][] = $row['ip'];
                        $id1[] = $row['id'];
                    }
                }

                if ($id0){
                    ZhConn::getConn()->table(DbName::IP_LOGS)->where('id','in',$id0)->update(['is_deduplicated' => 0]);
                }

                if ($id1){
                    ZhConn::getConn()->table(DbName::IP_LOGS)->where('id','in',$id1)->update(['is_deduplicated' => 1]);
                }
            }

        }

    }
}
